function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
		if(what == where[i]){
			a=true;
			break;
		}
	}
	return a;
}
function readablizeBytes(bytes,show) {
	if (bytes) {
	    var s = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
	    var e = Math.floor(Math.log(bytes)/Math.log(1024));
	    // return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
		var size = (bytes/Math.pow(1024, Math.floor(e))).toFixed(0);
 		show==1 ? size+=" "+s[e] : size;
	}else {
		size = 0;
	}
	return size;

}
function logs(s) {
	console.log(s);
}
function showloading() {
	// better ping to server first before send the data	
	$('#loading').css({'visibility':'visible'});
	// $(el).ajaxStart(function(){ $('#loading').css({'visibility':'visible'}); });
}
function hideloading() {
	var delay = {};
	delay = $.timer(500,function(){
		$('#loading').css({'visibility':'hidden'});
	});
	
}
function global_update(tmp_id) { // reuse function update and new
	var id = tmp_id;
	switch(id) {
		case "wup":
				$('#wup').attr("disabled","disabled");
				var web1 = $('#web1').val();
				var web2 = $('#web2').val();
				var match = new Array("web1","web2");
				var message = new Array("กรุณาใส่รหัสผ่าน","กรุณาใส่รหัสผ่านยืนยัน");
			break;
		case "tup":
				$('#tup').attr("disabled","disabled");
				var tor1 = $('#tor1').val();
				var tor2 = $('#tor2').val();
				var match = new Array("tor1","tor2");
				var message = new Array("กรุณาใส่รหัสผ่าน","กรุณาใส่รหัสผ่านยืนยัน");
			break;
		case "mup":
				$('#mup').attr("disabled","disabled");
				var my1 = $('#my1').val();
				var my2 = $('#my2').val();
				var uid = $('#uid').val();
				var match = new Array("my1","my2");
				var message = new Array("กรุณาใส่รหัสผ่าน","กรุณาใส่รหัสผ่านยืนยัน");
			break;
			
		default:break;

	}

	// Element valid notification
	var el_check = ''; var notice = ''; var el_name = ''; var check = 0;
	for (var i=0; i < match.length; i++) {
		el_check = $('#'+match[i]).val();
		if (el_check.length==0 || el_check==0 || el_check==".00") {
			check=1;
			el_name = match[i]; notice = i;
		}
	}
	// set focus on empty element
	if (check == 1) { 
		alert(message[notice]); $('#'+el_name).focus();
		// remove disable attribute button type input
		switch(id) {
			case "wup":
					$('#wup').removeAttr("disabled");
				break;
			case "tup":
					$('#tup').removeAttr("disabled");
				break;
			case "mup":
					$('#mup').removeAttr("disabled");
				break;
		default:break;
		}
	}
	else {
	
		switch(id) {
			 case "wup":
					showloading();
					var data = new Object();
						data.n	= new Date().getTime();
						data.web1	= web1;
						data.web2	= web2;
						if (web1==web2) {
								data.act = id;
							var str = $.toJSON(data); 
							$.post('jpost', { str: str }, function(result){
								var obj = eval('('+result+')');
								if (obj.f=="1") {
									alert("อัพเดทรหัสผ่านสมบูรณ์");
								}else {
									alert("อัพเดทรหัสผ่านไม่สำเร็จ\nกรุณาลองใหม่");
								}

							});
						}else {
							alert("รหัสผ่านสองช่องไม่เหมือนกัน");
							$('#web1').focus();
						}
						$('#wup').removeAttr("disabled");
					hideloading();
					
			 	break;

			 case "tup":
					showloading();
					var data = new Object();
						data.n	= new Date().getTime();
						data.tor1	= tor1;
						data.tor2	= tor2;
						if (tor1==tor2) {
								data.act = id;
							var str = $.toJSON(data); 
							$.post('jpost', { str: str }, function(result){
								var obj = eval('('+result+')');
								if (obj.f=="1") {
									alert("อัพเดทรหัสผ่านสมบูรณ์");
								}else {
									alert("อัพเดทรหัสผ่านไม่สำเร็จ\nกรุณาลองใหม่");
								}

							});
						}else {
							alert("รหัสผ่านสองช่องไม่เหมือนกัน");
							$('#tor1').focus();
						}
						$('#tup').removeAttr("disabled");
					
					hideloading();

			 	break;
			 case "mup":
					showloading();
					var data = new Object();
						data.n	= new Date().getTime();
						data.my1	= my1;
						data.my2	= my2;
						if (my1==my1) {
								data.act = id;
								data.uid = uid;
							var str = $.toJSON(data); 
							$.post('jpost', { str: str }, function(result){
								var obj = eval('('+result+')');
								if (obj.f=="1") {
									alert("อัพเดทรหัสผ่านสมบูรณ์");
									window.top.location="login";
								}else {
									alert("อัพเดทรหัสผ่านไม่สำเร็จ\nกรุณาลองใหม่");
								}

							});
						}else {
							alert("รหัสผ่านสองช่องไม่เหมือนกัน");
							$('#my1').focus();
						}
						$('#mup').removeAttr("disabled");

					hideloading();

			 	break;
			default:break;
	
		}
	}
}


$(document).ready(function() {
	
	if ( $('.standard_form').length == 1 ) { 
		// if login/register/contact  , length is mean if this element is exist
		$.get("token",function(txt){
			$("#login_form").append('<input type="hidden" name="ts" value="'+txt+'" />');
		});
		$('#login_field').focus();
		$('#login_form').ajaxStart(function(){ 
			showloading();
		});
		function loginResponse(result) {
			var obj = eval('('+result+')');
			if (obj.success=="1") {
				top.location="/staff";
			}
			if (obj.failed=="1") {
				$('#login_failed').css({"visibility":"visible", "position":"relative"});
				// $('#user_submit').attr("value","เข้าใช้งาน"); 
				if ($('#login_field').val().length>1) {
					$('#password_field').focus();  // Refocus on password field
				}else {
					$('#login_field').focus();  // Refocus on login field if empty string
				}
			}
			if (obj.expire=="1") {
				alert("time expire plese reload page and try again.");
			}
			hideloading();
		}
		$('#login_form').ajaxForm( { url: 'session', success: loginResponse, datatype: ($.browser.msie) ? "text" : "xml" });
	}

	// ******************************************************************************************************************
	
	$('a#edit_my').click(function() {
		var def = "show_my";
		var alel = $('#rightboard > div');
		for (var i=0; i < alel.length; i++) {
			// logs(alel[i].id);
			if (def==alel[i].id) {
				$('div#'+def).show();
			}else {
				$('div#'+alel[i].id).hide();
			}
		};
		$('input#cat_rows').val("");
		return(false);
	});
	$('a#edit_user').click(function() {
		var def = "show_user";
		var alel = $('#rightboard > div');
		for (var i=0; i < alel.length; i++) {
			// logs(alel[i].id);
			if (def==alel[i].id) {
				$('div#'+def).show();
			}else {
				$('div#'+alel[i].id).hide();
			}
		};
		$('input#com_rows').val("");
		return(false);
	});

	$('input:[type=password]').blur(function() {
		// logs("AAA");

		if (this.value.length>0 && this.value.length<5) {
			alert("กรุณาตั้งรหัสผ่านให้มีความยาว\nมากกว่า 5 ตัวอักษร");
		};
	});
	$('input:[type=button]').click(function() {
		global_update(this.id);
	});
});
